package com.nagarro.util;

public class Validator {

	/**
	 * returns true if the parameter is greater than equal to 0
	 * @param intValue String representation of integer
	 * @return true if intValue is greater than equal to 0, false otherwise
	 */
	public static boolean isPositiveInt(final String intValue, final String errMessage) {
		boolean valid = false;
		if (isNotNull(intValue,errMessage)) {
			try {
				int value = Integer.parseInt(intValue);
				if (value >= 0) {
					valid = true;
				} else {
					valid = false;
				}
			} catch (NumberFormatException e) {
				valid = false;
			}
		}
		if(!valid && (!errMessage.equals(null))) {
			System.err.println(errMessage);
		}
		return valid;
	}
	
	/**
	 * returns true if the parameter is greater than equal to 0
	 * @param doubleValue String representation of double
	 * @return true if doubleValue is greater than equal to 0, false otherwise
	 */
	public static boolean isPositiveDouble(final String doubleValue, final String errMessage) {
		boolean valid = false;
		if (isNotNull(doubleValue,errMessage)) {
			try {
				double value = Double.parseDouble(doubleValue);
				if (value >= 0) {
					valid = true;
				} else {
					valid = false;
				}
			} catch (NumberFormatException e) {
				valid = false;
			}
		}
		if(!valid && (!errMessage.equals(null))) {
			System.err.println(errMessage);
		}
		return valid;
	}

	/**
	 * check if an object is null or not
	 * @param o object to be checked for null condition
	 * @return false if item is null, true otherwise
	 */
	public static boolean isNotNull(final Object o, final String errMessage) {
		boolean valid;
		if (o == null) {
			valid = false;
		} else {
			valid = true;
		}
		if(!valid && (!errMessage.equals(null))) {
			System.err.println(errMessage);
		}
		return valid;
	}
}
